package com.tools.taskmanage
{

	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.utils.getTimer;

	import mx.collections.ArrayList;
	import mx.core.FlexGlobals;

	/**
	 * 任务管理器,主要是为了保证系统中只有一个Timer在跑,多个Timer跑起来时间会很不准
	 * @author Boder
	 * @Time 2011-9-6 16:17:33
	 */
	public class TaskManager
	{
		private static var __imTask  :  TaskManager;

		private var taskArr  :  ArrayList  =  new ArrayList(); //任务库
		private var isRunning  :  Boolean  =  false; //正在执行

		private var app  :  DisplayObject;

		public function TaskManager()
		{
			if ( __imTask != null )
				throw new Error( "TaskManager 只能实例化一次." );
			app  =  FlexGlobals.topLevelApplication as DisplayObject;
		}

		/**
		 * 执行任务
		 * @param event
		 *
		 */
		protected function doTimerTask( event  :  Event )  :  void
		{
			var time  :  int   =  getTimer();
			var i_len  :  int  =  taskArr.length - 1;
			var tObj  :  TaskObj;
			for ( var i  :  int  =  i_len; i > -1; i-- )
			{
				tObj  =  taskArr.getItemAt( i ) as TaskObj;
				tObj.doTask( time );
			}
		}

		/**
		 * 根据任务名称返回任务对象
		 * @param name
		 * @return
		 *
		 */
		public function getTaskByName( name  :  String )  :  TaskObj
		{
			var i_len  :  int  =  taskArr.length - 1;
			var tObj  :  TaskObj;
			for ( var i  :  int  =  i_len; i > -1; i-- )
			{
				tObj  =  taskArr.getItemAt( i ) as TaskObj;
				if ( tObj.name == name )
				{
					return tObj;
				}
			}

			return null;
		}

		/**
		 * 添加一个任务
		 * @param task 任务对象
		 *
		 */
		public function addTask( taskName  :  String, fn  :  Function, deplay  :  Number  =  1000, times  :  int  =  -1, pms  :  Array  =  null )  :  void
		{
			removeTask( taskName );

			var task  :  TaskObj  =  new TaskObj( fn, deplay, times, pms );
			task.name  =  taskName;
			taskArr.addItem( task );
			if ( !isRunning )
			{
				app.addEventListener( Event.ENTER_FRAME, doTimerTask );
				isRunning  =  true;
			}
			task.startTime  =  getTimer();

		}

		/**
		 * 删除一个任务
		 * @param task 要删除的任务对象
		 */
		public function removeTask( taskName  :  String )  :  void
		{
			var tobj  :  TaskObj  =  getTaskByName( taskName );

			if ( !tobj )
			{
				return;
			}
			//trace("删除----" + tobj.name + "任务");
			taskArr.removeItem( tobj );
			if ( tobj )
			{
				tobj.fun  =  null;

				if ( tobj.params )
				{
					tobj.params.splice( 0 );
				}
				tobj.params  =  null;
				tobj  =  null;
			}

			var len  :  int  =  taskArr.length;
			if ( len <= 0 && isRunning )
			{
				app.removeEventListener( Event.ENTER_FRAME, doTimerTask );
				isRunning  =  false;
			}
		}

		/**
		 * 清理注销
		 *
		 */
		public function cancel()  :  void
		{
			if ( !isRunning )
			{
				return;
			}
			app.removeEventListener( Event.ENTER_FRAME, doTimerTask );
			isRunning  =  false;
			var i_len  :  int  =  taskArr.length - 1;
			var tobj  :  TaskObj;
			for ( var i  :  int  =  i_len; i > -1; i-- )
			{
				tobj  =  taskArr.removeItemAt( 0 ) as TaskObj;
				tobj.fun  =  null;
				if ( tobj.params )
					tobj.params.splice( 0 );
				tobj.params  =  null;
				tobj  =  null;
			}
		}

		/**
		 * 获取实例
		 *
		 */
		public static function getInstance()  :  TaskManager
		{
			if ( !__imTask )
				__imTask  =  new TaskManager();
			return __imTask;
		}
	}
}
